package com.yc.url;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class BookSpider {
    public static void main(String[] args) throws IOException {
        BookSpider zs=new BookSpider();


     /*   for (int i = 1; i <= 5; i++) {
            zs.capJobs("http://www.0734zpw.com/jobs?page="+i);
        }*/
       // 1.zs.capJobs("https://www.jiushujie.com/");
      //  zs.capJobss("https://www.jiushujie.com/sell?q=计算机"); 数学、经济、.....
       // zs.capJobss("https://www.jiushujie.com/sell?q=法律");
    }


    //收集 job 链接
    public void capJobs(String address) throws IOException {
        URL url=new URL(address);
        Document document = Jsoup.parse(url, 5000);
        Elements as= document.select(".book_name>a");
        System.out.println(as);
        for(Element element:as){
            //
            String href=element.attr("href");
            System.out.println(href);// /sell/458160614 /：表示当前路径
            String jobAddress=url.getProtocol()+"://"+url.getHost()+href;
            System.out.println(jobAddress);//https://www.jiushujie.com/sell/458160614

            capJob(jobAddress);

        }

    }
    public void capJobss(String address) throws IOException {
        URL url=new URL(address);
        Document document = Jsoup.parse(url, 5000);
        Elements as= document.select(".title>a");
        System.out.println(as);
        for(Element element:as){
            //
            String href=element.attr("href");
            System.out.println(href);// /sell/458160614 /：表示当前路径
            String jobAddress=url.getProtocol()+"://"+url.getHost()+href;
            System.out.println(jobAddress);//https://www.jiushujie.com/sell/458160614

            capJob(jobAddress);

        }

    }




    public void capJob(String address) throws IOException {
        DBHelper db=new DBHelper();
        String id= address.replaceAll("[^0-9]", "");

        System.out.println(id);
        String sql1="select count(*) cnt from book where id = ?";
        List<Map<String, Object>> list= db.findMultiple(sql1,id);
        if(Integer.parseInt(""+list.get(0).get("cnt"))>0){
            System.out.println("该 job"+id+"已经采集过了");
            return;
        }

        Document document = Jsoup.parse(new URL(address), 5000);

        Element  element = document.selectFirst(".book_item>.info>h2");
        System.out.println(element);
        String bookname = element.text();
        element= document.selectFirst(".content_right>.book_item>.info>.detail");
        String bookdetail = element.text();



        element= document.selectFirst(".content_right>.book_item>.book_introduction_container");
        System.out.println(element);
        if(element==null){
            return;
        }
        String bookinfo = element.html();

        System.out.println("jobname = " + bookname);
        System.out.println("money = " + bookdetail);
        System.out.println("describe = " + bookinfo);

        String sql="insert into book values(?,?,?,?)";
        db.update(sql,id,bookname,bookdetail,bookinfo);
    }
}
